package cn.kduck.plugin.idea.generate.impl;

import cn.kduck.plugin.idea.generate.CodeGenerator;
import cn.kduck.plugin.idea.utils.GeneratorUtils;
import cn.kduck.plugin.idea.utils.StringUtils;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.search.GlobalSearchScope;

public class ConstantBasedGenerator implements CodeGenerator {


    @Override
    public String code() {
        return "CONSTANT";
    }

    @Override
    public void updateCode(Project project,PsiClass javaClass, PsiField[] selectFields) {
        PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project);

        //！！！！！涉及到类的构造构成，不要随便更换顺序！！！！！

        //处理构造器（默认构造器和map参数构造器）
        GeneratorUtils.createConstructor(elementFactory,project, javaClass);


        PsiClass parentClass = GeneratorUtils.getValueMapClass(project);
        boolean isValueMap = GeneratorUtils.isValueMap(parentClass);

        //处理继承类
        GeneratorUtils.addExtendClass(elementFactory,javaClass,parentClass);

        //处理字段声明及对应的getter及setter方法
        //将属性转变为PUBLIC STATIC FINAL修饰符，并且属性定义命名也进行转换
        processFields(elementFactory, project, javaClass, selectFields, isValueMap);

    }

    /**
     * 处理字段的声明已经对应的getter和setter方法
     */
    private void processFields(PsiElementFactory elementFactory, Project project, PsiClass javaClass, PsiField[] selectFields, boolean isValueMap) {
        PsiClassType stringType = PsiClassType.getTypeByName(String.class.getName(), project, GlobalSearchScope.allScope(project));
        for (PsiField selectField : selectFields) {
            String newName = StringUtils.upperCaseWithSeparator(selectField.getName(), "_");
            PsiField newField = elementFactory.createField(newName, stringType);
            PsiModifierList modifierList = newField.getModifierList();
            modifierList.setModifierProperty(PsiModifier.PUBLIC, true);
            modifierList.setModifierProperty(PsiModifier.FINAL, true);
            modifierList.setModifierProperty(PsiModifier.STATIC, true);

            if(selectField.getDocComment() != null){
                newField.addBefore(selectField.getDocComment(), newField.getFirstChild());
            }else {
                String comment = GeneratorUtils.getCommentForField(selectField);
                if(comment != null){
                    PsiDocComment fieldComment = elementFactory.createDocCommentFromText("/** " + comment + " */");
                    newField.addBefore(fieldComment, newField.getFirstChild());
                }
            }

            PsiExpression initValue = elementFactory.createExpressionFromText("\"" + selectField.getName() + "\"", newField);
            newField.setInitializer(initValue);

            //处理getter和setter方法
            javaClass.add(GeneratorUtils.createSetterMethod(elementFactory,selectField, isValueMap));
            javaClass.add(GeneratorUtils.createGetterMethod(elementFactory,selectField, isValueMap));

            selectField.replace(newField);
        }
    }

    @Override
    public void newCode(Project project,PsiClass javaClass, PsiField[] selectFields) {

    }

}
